﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interface{
    public class Logika{
        private northwindDataContext db;
        private List<Tuple<Employee, decimal>> all;
        public Logika(){
            db = new northwindDataContext();
            all = getAll();
        }
        public List<string> filter(decimal min, decimal max){
            return filterSum(min, max, all);
        }
        private List<Tuple<Employee, decimal>> getAll(){
            var orderGroups =
                from o in db.Orders
                join d in db.Order_Details on o.OrderID equals d.OrderID into oDet
                select new { ord = o, prize = oDet.Sum(i => i.Quantity * i.UnitPrice) };
            var empWithSum =
                from e in db.Employees
                let sum = (
                    from o in orderGroups
                    where e.Orders.Contains(o.ord)
                    select o.prize
                ).Sum()
                orderby sum
                select new Tuple<Employee, decimal>(e, sum);
            return empWithSum.ToList();
        }
        protected List<string> filterSum(decimal min, decimal max, List<Tuple<Employee, decimal>> employees){
            var filtered = (
                from e in employees
                where e.Item2 > min && e.Item2 < max
                select String.Format(
                    "{0} {1}    -- {2:N2}",
                    e.Item1.FirstName,
                    e.Item1.LastName,
                    e.Item2
                )
            ).ToList();
            if (filtered.Count() == 0) filtered.Add(" -- no records for such conditions -- ");
            return filtered;
        }
    }
}
